home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- FILE: ArrowCDEF.c
- CREATED: March 16, 1994
- AUTHOR: David Hay
- VERSION: 1.1
-
- Copyright © 1994 David Hay
-
- ArrowCDEF may be freely distributed, modified, included in any application
- or CD-ROM compilation, etc. as long as the following restrictions are met:
- (1) This entire copyright notice remains intact when the source is
- redistributed, modified, etc.
- (2) The copyright notice in the CDEF itself (the "copyright" string)
- remains intact.
-
- David Hay hereby disclaims all warranties relating to this software,
- whether express or implied, including without limitation any implied
- warranties of merchantability or fitness for a particular purpose. David
- Hay will not be liable for any special, incidental, consequential,
- indirect or similar damages due to loss of data or any other reason, even
- if David Hay or an agent of his has been advised of the possibility of
- such damages. In no event shall David Hay be liable for any damages,
- regardless of the form of the claim. The person using the software bears
- all risk as to the quality and performance of the software.
-
- US Governement:
- Government End Users: If you are acquiring the Software and fonts
- on behalf of any unit or agency of the United States Government, the
- following provisions apply. The Government agrees:
- (i) if the Software and fonts are supplied to the Department of
- Defense (DoD), the Software and fonts are classified as "Commercial
- Computer Software" and the Government is acquiring only "restricted rights"
- in the Software, its documentation and fonts as that term is defined in
- Clause 252.227-7013(c)(1) of the DFARS; and
- (ii) if the Software and fonts are supplied to any unit or agency
- of the United States Government other than DoD, the Government's rights in
- the Software, its documentation and fonts will be as defined in Clause
- 52.227-19(c)(2) of the FAR or, in the case of NASA, in Clause
- 18-52.227-86(d) of the NASA Supplement to the FAR.
-
- HOW TO USE ArrowCDEF:
-
- Implements an up-down arrow similar to those found in the color picker. It
- uses a set of four picts to determine how to draw the CDEF in each of it's
- four states. To use the CDEF, create an APIC resource that gives the
- resource ID's of the four picts to use for the plain arrow, the arrow with
- the up button pressed, the arrow with the down button pressed, and the
- arrow when it is inactive. The ID of the APIC resource should be put in
- the control's refCon field when the control is initialized. After the
- control is created, the reference constant may be used however you wish.
-
- Comments and questions are welcome:
- E-mail: hay@cs.colorado.edu
- US Mail: David Hay
- 117 Piedra Loop
- Los Alamos, NM 87544
-
- Thanks to Eddy J. Gurney and to all those that have expressed interest in
- this CDEF. Also, thanks to the regulars on comp.sys.mac.programmer for
- indirectly helping me untangle the mac toolbox by consistantly helping
- beginners like myself.
-
- CHANGE HISTORY:
- 1.0 -- Initial release
- 1.1 -- Modified to used GetResource instead of GetPicture and
- Get1Resource so we don't need to keep track of the resource
- file. Also changed the license to one less restrictive.
- *******************************************************************************/
-
- #include <SetUpA4.h>
- #include "ArrowCDEF.h"
-
- #define kInactive 255 /* part code indicating the control is inactive */
-
- const unsigned char copyright[] = "Arrow CDEF 1.1 ©1994 David Hay.";
-
- struct ControlResData /* The data we get from the resource */
- {
- short plainPictID; /* PICT to draw for an unhilited arrow */
- short upPictID; /* PICT to draw when the up arrow is pressed */
- short downPictID; /* PICT to draw then the down arrow is pressed */
- short inactivePictID; /* PICT to draw when the arrow is inactive */
- };
-
- typedef struct ControlResData ControlResData;
- typedef ControlResData **CntlResDataHndl;
-
-
- struct ControlData /* the data we store in the contrlData field of the control */
- {
- ControlResData picts; /* The PICTs to draw */
- };
-
- typedef struct ControlData ControlData;
- typedef ControlData **ControlDataHandle;
-
-
-
- pascal long main( short variation, ControlHandle theControl, short msg, long param )
- {
- long result; /* the result to return */
- ControlDataHandle control; /* the control data */
-
- RememberA0();
- SetUpA4();
-
- result = 0;
-
- switch ( msg )
- {
- case initCntl: /* initialize the arrow control data */
- {
- short arrowResID; /* ID of resource holding the pict info */
- CntlResDataHndl picts; /* res handle to hold which picts to use */
- Rect picRect; /* rectangle of the picture rect */
- PicHandle plainPict; /* plain picture determines size of control */
-
- arrowResID = GetCRefCon( theControl );
- picts = (CntlResDataHndl) GetResource( kArrowResType, arrowResID );
- control = (ControlDataHandle) NewHandleClear( sizeof( ControlData ) );
- BlockMove( *picts, *control, sizeof( ControlResData ) );
- (*theControl)->contrlData = (Handle)control;
- ReleaseResource( picts ); /* We copied it so we don't need it anymore */
-
- /* Get the plain picture to determine the size of the control.
- ** After resizing the control to the plain picture, release
- ** the picture
- **/
- plainPict = (PicHandle) GetResource( 'PICT',
- (*control)->picts.plainPictID );
- picRect = (*plainPict)->picFrame;
- SizeControl( theControl, picRect.right - picRect.left,
- picRect.bottom - picRect.top );
- ReleaseResource( plainPict );
- break;
- }
-
- case drawCntl: /* Draw the arrow control */
- {
- PicHandle thePict; /* the PICT to draw */
- unsigned char hilite; /* the current control hilite state */
- Rect drawRect; /* the rect to draw the PICT in */
- short pictID; /* the resource ID of the PICT */
- short curRes; /* the current resource file */
-
- if ( (*theControl)->contrlVis )
- {
- /* Initialize some stuff and grab some values out of the
- ** control structure so we don't have to constantly
- ** dereference the control handle
- **/
- thePict = NULL;
- hilite = (*theControl)->contrlHilite;
- control = (ControlDataHandle) (*theControl)->contrlData;
- drawRect = (*theControl)->contrlRect;
- switch( hilite )
- {
- case 0: /* no hiliting */
- pictID = (*control)->picts.plainPictID;
- break;
- case inUpButton: /* up arrow hilited */
- pictID = (*control)->picts.upPictID;
- break;
- case inDownButton: /* down arrow hilited */
- pictID = (*control)->picts.downPictID;
- break;
- case kInactive: /* inactive control */
- pictID = (*control)->picts.inactivePictID;
- break;
- default: /* any other part codes don't apply */
- pictID = -1;
- break;
- }
- if ( pictID >= 0 ) /* This will probably always be true, but */
- { /* better safe than sorry! */
- thePict = (PicHandle) GetResource( 'PICT', pictID );
- if ( thePict ) /* We got the picture handle */
- {
- if ( hilite == kInactive ) /* Erase the arrow only when… */
- EraseRect( &drawRect ); /* …it needs to be deactivated */
- DrawPicture( thePict, &drawRect );
- ReleaseResource( thePict );
- }
- }
- }
- break;
- }
-
- case testCntl: /* Determine which part of the arrow the mouse is in */
- {
- Point mousePoint; /* where the mouse is */
- Rect upRect; /* rect comprimising the up arrow */
- Rect downRect; /* rect comprimising the down arrow */
- Rect plainRect; /* the entire arrow rect */
-
- plainRect = (*theControl)->contrlRect;
- mousePoint.v = HiWord( param );
- mousePoint.h = LoWord( param );
-
- /* If the mouse point is in the control and it is active,
- ** determine which part the mouse went down in
- **/
- if ( (*theControl)->contrlHilite != kInactive && PtInRect( mousePoint, &plainRect ) )
- {
- /* Assume the up arrow and down arrow each take
- ** half the control
- **/
- upRect = plainRect;
- upRect.bottom -= (plainRect.bottom - plainRect.top) / 2;
- downRect = plainRect;
- downRect.top = upRect.bottom - 1;
-
- if ( PtInRect( mousePoint, &upRect ) ) /* up arrow pressed */
- {
- result = inUpButton;
- }
- else if ( PtInRect( mousePoint, &downRect ) ) /* down arrow pressed */
- {
- result = inDownButton;
- }
- }
- break;
- }
-
- case calcCRgns: /* This is the message sent if using 24-bit addressing */
- param = (long) StripAddress( (Ptr)param ); /* Mask off the high byte if necessary */
- /* FALL THROUGH ON PURPOSE! */
-
- case calcCntlRgn: /* Under 32-bit addressing we get this message */
- RectRgn( (RgnHandle)param, &(*theControl)->contrlRect );
- break;
-
- case dispCntl: /* Free the data stored in the control */
- if ( (*theControl)->contrlData );
- {
- DisposeHandle( (*theControl)->contrlData );
- (*theControl)->contrlData = NULL;
- }
- break;
-
- #if 0
- case posCntl:
- case thumbCntl:
- case dragCntl:
- case autoTrack:
- case undoDev:
- case cutDev:
- case copyDev:
- case pasteDev:
- case clearDev:
- case cursorDev:
- case calcThumbRgn:
- break; /* We don't respond to any of these messages */
- #endif
-
- default: /* Any other messages we don't handle or which aren't defined yet */
- break;
- }
-
- RestoreA4();
-
- return( result );
- }
-